package com.gmail.huxiaowi.cblog.dao.impl.jdo;

import java.util.Collection;

import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.JdoCallback;

import com.gmail.huxiaowi.cblog.dao.IArticleDao;
import com.gmail.huxiaowi.cblog.entity.Article;
import com.gmail.huxiaowi.cblog.entity.Category;

/**
 * Class ArticleDaoJDOImpl
 * 
 * @author  huxw
 * @version $Revision:1.0.0, $Date:2010-4-29 下午05:45:36$
 */
public class ArticleDaoJDOImpl extends GenericDaoJDOImpl<Article> implements IArticleDao {

    /* (non-Javadoc)
     * @see com.gmail.huxiaowi.cblog.dao.ArticleDao#queryByCategory(com.gmail.huxiaowi.cblog.entity.Category)
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Collection<Article> queryByCategory(final Category category, final long start, final long end) {
        // TODO Auto-generated method stub
        return jt.executeFind(new JdoCallback() {
            
            public Object doInJdo(PersistenceManager pm) throws JDOException {
                Query query = pm.newQuery("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article WHERE categoryId==" + category.getId());
                query.setOrdering("dateline DESC");//日期倒序
                query.setRange(start, end);//分页显示
                return query.execute();
            }
        });
//        return super.jt.find("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article WHERE category==:category ", new Object[]{category});
    }

    /* (non-Javadoc)
     * @see com.gmail.huxiaowi.cblog.dao.IArticleDao#queryByUsername(com.gmail.huxiaowi.cblog.entity.User, long, long)
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Collection<Article> queryAll(final long start, final long end) {
        
        return jt.executeFind(new JdoCallback() {
            
            public Object doInJdo(PersistenceManager pm) throws JDOException {
                Query query = pm.newQuery("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article");
                query.setOrdering("dateline DESC");
                query.setRange(start, end);
                return query.execute();
            }
        });
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Collection<Article> queryByCategory(final Long categoryId) {
        return jt.executeFind(new JdoCallback() {
            
            public Object doInJdo(PersistenceManager pm) throws JDOException {
                Query query = pm.newQuery("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article WHERE categoryId==" + categoryId);
                return query.execute();
            }
        });
//        return jt.find("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article WHERE category.id==" + categoryId);
    }

    @Override
    public Article queryByPostName(final String postName) {
        @SuppressWarnings("unchecked")
        Collection<Article> collArticle = jt.find("SELECT FROM com.gmail.huxiaowi.cblog.entity.Article WHERE postName==" + postName);
        return (collArticle == null || collArticle.size() == 0) ? null : collArticle.iterator().next();
    }

}
